package com.app.beu.core.renderer;

import java.io.IOException;

import tsm.constant.ClientConstant;
import tsm.model.Buddy;
import tsm.network.BeUSession;

import com.core.tsm.Button;
import com.core.tsm.Component;
import com.core.tsm.Image;
import com.core.tsm.Label;
import com.core.tsm.List;
import com.core.tsm.list.ListCellRenderer;

public class ContactListRenderer extends Label implements ListCellRenderer {
	private Image level0;
	private Image level1;
	private Image level2;
	private Image level3;
	private Image level4;
	private Image level5;
	private Image levelKing;
	private Image offline;

	public ContactListRenderer() {
		super("");
		try {
			level0 = Image.createImage("/level_0.png");
			offline = Image.createImage("/off.png");
		} catch (IOException ex) {
		}
		setUIID("ListRenderer");
	}

	public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
		Buddy buddy = (Buddy) value;

		if (isSelected) {
			if (buddy.getAvatarId() == -1) {
				BeUSession.requestLazyBuddyInfo(buddy.getUserId());
			}
			if (buddy.getStatus() == ClientConstant.ONLINE) {
				switch (buddy.getLevel()) {
				case 1:
					if (level1 == null) {
						try {
							level1 = Image.createImage("/level_1.png");
						} catch (IOException ex) {
						}
					}
					buddy.getStatusLabel().setIcon(level1);
					break;
				case 2:
					if (level2 == null) {
						try {
							level2 = Image.createImage("/level_2.png");
						} catch (IOException ex) {
						}
					}
					buddy.getStatusLabel().setIcon(level2);
					break;
				case 3:
					if (level3 == null) {
						try {
							level3 = Image.createImage("/level_3.png");
						} catch (IOException ex) {
						}
					}
					buddy.getStatusLabel().setIcon(level3);
					break;
				case 4:
					if (level4 == null) {
						try {
							level4 = Image.createImage("/level_4.png");
						} catch (IOException ex) {
						}
					}
					buddy.getStatusLabel().setIcon(level4);
					break;
				case 5:
					if (level5 == null) {
						try {
							level5 = Image.createImage("/level_5.png");
						} catch (IOException ex) {
						}
					}
					buddy.getStatusLabel().setIcon(level5);
					break;
				case 100:
					if (levelKing == null) {
						try {
							levelKing = Image.createImage("/level_king.png");
						} catch (IOException ex) {
						}
					}
					setIcon(levelKing);
					break;
				default:
					buddy.getStatusLabel().setIcon(level0);
					break;
				}
			} else {
				buddy.getStatusLabel().setIcon(offline);
			}
			buddy.getStatusLabel().setText(buddy.getUsername());
			getStyle().setBgColor(0x138ef6);
			getStyle().setBgTransparency(50);
			return buddy.getAvatarContainer();
		}
		if (buddy.getStatus() == ClientConstant.ONLINE) {
			switch (buddy.getLevel()) {
			case 1:
				if (level1 == null) {
					try {
						level1 = Image.createImage("/level_1.png");
					} catch (IOException ex) {
					}
				}
				setIcon(level1);
				break;
			case 2:
				if (level2 == null) {
					try {
						level2 = Image.createImage("/level_2.png");
					} catch (IOException ex) {
					}
				}
				setIcon(level2);
				break;
			case 3:
				if (level3 == null) {
					try {
						level3 = Image.createImage("/level_3.png");
					} catch (IOException ex) {
					}
				}
				setIcon(level3);
				break;
			case 4:
				if (level4 == null) {
					try {
						level4 = Image.createImage("/level_4.png");
					} catch (IOException ex) {
					}
				}
				setIcon(level4);
				break;
			case 5:
				if (level5 == null) {
					try {
						level5 = Image.createImage("/level_5.png");
					} catch (IOException ex) {
					}
				}
				setIcon(level5);
				break;
			case 100:
				if (levelKing == null) {
					try {
						levelKing = Image.createImage("/level_king.png");
					} catch (IOException ex) {
					}
				}
				setIcon(levelKing);
				break;
			default:
				setIcon(level0);
				break;
			}
		} else {
			setIcon(offline);
		}
		setText(buddy.getUsername());
		setFocus(false);
		getStyle().setBgTransparency(255);
		getStyle().setPadding(Button.TOP, 0);
		getStyle().setMargin(Button.TOP, 0);
		getStyle().setPadding(Button.BOTTOM, 1);
		getStyle().setMargin(Button.BOTTOM, 1);
		getStyle().setPadding(Button.LEFT, 0);
		getStyle().setMargin(Button.LEFT, 0);
		getStyle().setPadding(Button.RIGHT, 0);
		getStyle().setMargin(Button.RIGHT, 0);
		getStyle().setBgColor(0xd2d4d3);

		return this;
	}

	public Component getListFocusComponent(List list) {
		setIcon(null);
		setText("");
		setFocus(true);
		getStyle().setBgTransparency(255);
		getStyle().setPadding(Button.TOP, 0);
		getStyle().setMargin(Button.TOP, 0);
		getStyle().setPadding(Button.BOTTOM, 1);
		getStyle().setMargin(Button.BOTTOM, 1);
		getStyle().setPadding(Button.LEFT, 0);
		getStyle().setMargin(Button.LEFT, 0);
		getStyle().setPadding(Button.RIGHT, 0);
		getStyle().setMargin(Button.RIGHT, 0);
		getStyle().setBgColor(0x138ef6);
		return this;
	}
}
